/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.edu.uw.come.mooba;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.List;
import java.util.zip.ZipFile;
import pl.edu.uw.come.mooba.domain.BackupInfo;
import pl.edu.uw.come.mooba.domain.CourseInfo;
import pl.edu.uw.come.mooba.domain.Log;
import pl.edu.uw.come.mooba.domain.User;
import pl.edu.uw.come.mooba.domain.exceptions.BackupDescFileParsingException;
import pl.edu.uw.come.mooba.domain.exceptions.InvalidMoodleBackupArchiveException;

/**
 *
 * @author Piotr Kochański
 */
public class Main {
    private static DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, InvalidMoodleBackupArchiveException, BackupDescFileParsingException {
        System.out.println("Version 0.2");
        System.out.println("==============================================");
        System.out.println("");
        for (String fileName : args) {
            DomBackupParser dbp = new DomBackupParser(new ZipFile(new File(fileName)));
            CourseInfo ci = dbp.getCourseInfo();
            List<User> students = dbp.getUsers("student");
            List<User> users = dbp.getUsers(null);
            printCourseInfo(dbp.getBackupInfo(), ci);
            System.out.println("NUMBER OF ALL USERS: " + dbp.getNumberOfUsers(null));
            System.out.println("NUMBER OF STUDENTS: " + dbp.getNumberOfUsers("student"));
            System.out.println("25 FIRST LOG ENTRIES: ");
            if(dbp.getLogs() != null && dbp.getLogs().size() > 0){
                List<Log> logs = dbp.getLogs();
                for (int i = 0; i < 25; i++) {
                    System.out.println(df.format(logs.get(i).getTime().getTime()) + ":" + UserManager.findUserById(students, logs.get(i).getUserid()));
                }
            }else{
                System.out.println("NO LOGS IN BACKUP");
            }
            System.out.println("==============================================");
            logUsers(students, users, fileName + ".log", ci);
        }
    }

    public static void printCourseInfo(BackupInfo bi, CourseInfo ci) {
        System.out.println("ARCHIVE FILE NAME: " + bi.getArchFileName());
        System.out.println("BACKUP DATE: " + df.format(bi.getBackupDate().getTime()));
        System.out.println("COURSE CATEGORY/NAME: " + ci.getCategoryName() + "/" + ci.getFullname());
    }
    public static void logUsers(List<User> students, List<User> users, String fileName, CourseInfo ci) throws FileNotFoundException {
        File f = new File(fileName);
        PrintWriter pw = new PrintWriter(f);

        DateFormat dfShort = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
        pw.println("Students on course " + ci.getCategoryName() + "/" + ci.getFullname() );
        pw.println();
        for (User user : students) {
            pw.print(user.getFirstname() + " " + user.getLastname() + " (" + user.getEmail() + ")");
            pw.println(" LOGGED FOR THE FIRST TIME " + dfShort.format(user.getFirstaccess().getTime()) + " LAST LOGGED " +
                    dfShort.format(user.getLastlogin().getTime()));
        }
        pw.println();
        pw.println("Non-students on course " + ci.getCategoryName() + "/" + ci.getFullname() );
        users.removeAll(students);
        for (User user : users) {
            pw.print(user.getFirstname() + " " + user.getLastname() + " (" + user.getEmail() + ")");
            pw.println(" LOGGED FOR THE FIRST TIME " + dfShort.format(user.getFirstaccess().getTime()) + " LAST LOGGED " +
                    dfShort.format(user.getLastlogin().getTime()));
        }
        pw.close();
    }
}
